# 药品管理相关的测试用例
# 导包
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
import unittest
from tools.tools import Tools
from ddt import ddt, data
from tools.read_excel import ReadTestData
from page_model import login

drug = ReadTestData('./test_data/mms_test_data.xls')
drug_yp_t = drug.read_excel('dl_t')  # 读取录入药品功能的正向测试数据
drug_gk_t = drug.read_excel('gk_t')  # 读取录入顾客功能的正向测试数据
drug_jbr_t = drug.read_excel('jbr_t')  # 读取录入顾客功能的正向测试数据

@ddt
class MmsDrugTest(unittest.TestCase):

    def setUp(self) -> None:
        self.dr = webdriver.Chrome()
        self.dr.implicitly_wait(5)
        self.dr.maximize_window()
        self.lg = login.Login(self.dr)  # 调用登录页面类

    def tearDown(self) -> None:
        self.dr.quit()

    # 药品录入的测试用例
    @data(*drug_yp_t)
    def testDrugInput(self, drug):
        self.lg.login(drug)
        # 点击录入按钮
        self.dr.find_element(By.XPATH, '//*[@id="accordion"]/div[2]/div[1]').click()
        # 点击药品录入按钮
        self.dr.find_element(By.XPATH, '//*[@id="EC"]/a[3]').click()
        # # 对当前页面进行缩放
        # js = 'document.getxxxxxxxxxxxxxxxxxx'  # js
        # self.dr.execute_script(js)
        # 定位并输入数据
        self.dr.find_element(By.XPATH, '//*[@id="EMMform"]/div[1]/span[1]/input[1]').send_keys(drug['num'])
        self.dr.find_element(By.XPATH, '//*[@id="EMMform"]/div[1]/span[2]/input[1]').send_keys(drug['method'])
        self.dr.find_element(By.XPATH, '//*[@id="EMMform"]/div[2]/span/textarea').send_keys(drug['d_name'])
        self.dr.find_element(By.XPATH, '//*[@id="EMMform"]/div[3]/span/textarea').send_keys(drug['effect'])
        # 点击录入按钮
        time.sleep(0.2)
        self.dr.find_element(By.LINK_TEXT, '录入').click()
        time.sleep(1)
        # 断言- 查看弹窗中是否弹出录入成功
        try:
            msg = self.dr.find_element(By.XPATH, '//*[@class="messager-body panel-body panel-body-noborder window-body"]/div[0]').text
            ex = '保存成功'
            self.assertEqual(ex, msg, '出错啦')
        except Exception as e:
            Tools.save_screenshot(self.dr, 'drug_add')
            raise e

    # 顾客的录入测试用例
    @data(*drug_gk_t)
    def testGkInput(self, drug):
        self.lg.login(drug)
        # 点击录入按钮
        self.dr.find_element(By.XPATH, '//*[@id="accordion"]/div[2]/div[1]').click()
        # 点击顾客录入按钮
        self.dr.find_element(By.XPATH, '//*[@id="EC"]/a[1]').click()
        # 定位并输入数据
        self.dr.find_element(By.XPATH, '//*[@id="ECMform"]/div/div[1]/span[1]/input[1]').send_keys(drug['num'])
        self.dr.find_element(By.XPATH, '//*[@id="ECMform"]/div/div[1]/span[2]/input[1]').send_keys(drug['gk_name'])
        self.dr.find_element(By.XPATH, '//*[@id="ECMform"]/div/div[1]/span[3]/input[1]').send_keys(drug['sex'])
        self.dr.find_element(By.XPATH, '//*[@id="ECMform"]/div/div[2]/span[1]/input[1]').send_keys(drug['age'])
        self.dr.find_element(By.XPATH, '//*[@id="ECMform"]/div/div[2]/span[2]/input[1]').send_keys(drug['tel'])
        self.dr.find_element(By.XPATH, '//*[@id="ECMform"]/div/div[2]/span[3]/input[1]').send_keys(drug['jbr'])
        self.dr.find_element(By.XPATH, '//*[@id="ECMform"]/div/div[3]/span[1]/input[1]').send_keys(drug['day'])
        self.dr.find_element(By.XPATH, '//*[@id="ECMform"]/div/div[3]/span[2]/input[1]').send_keys(drug['buy_yp'])
        self.dr.find_element(By.XPATH, '//*[@id="ECMform"]/div/div[4]/span/input[1]').send_keys(drug['home'])
        self.dr.find_element(By.XPATH, '//*[@id="ECMform"]/div/div[5]/span[1]/textarea').send_keys(drug['zz'])
        self.dr.find_element(By.XPATH, '//*[@id="ECMform"]/div/div[5]/span[2]/textarea').send_keys(drug['bz'])
        # 点击录入按钮
        time.sleep(0.2)
        self.dr.find_element(By.LINK_TEXT, '录入').click()
        time.sleep(1)
        # 断言- 查看弹窗中是否弹出录入成功
        try:
            msg = self.dr.find_element(By.XPATH, '//*[@class="messager-body panel-body panel-body-noborder window-body"]/div[0]').text
            ex = '保存成功'
            self.assertEqual(ex, msg, '出错啦')
        except Exception as e:
            Tools.save_screenshot(self.dr, 'drug_add')
            raise e

    # jbr的录入测试用例
    @data(*drug_jbr_t)
    def testJbrInput(self, drug):
        self.lg.login(drug)
        # 点击录入按钮
        self.dr.find_element(By.XPATH, '//*[@id="accordion"]/div[2]/div[1]').click()
        # 点击经办人录入按钮
        self.dr.find_element(By.XPATH, '//*[@id="EC"]/a[2]/span/span[1]').click()
        # 定位并输入数据
        self.dr.find_element(By.XPATH, '//*[@id="EAMform"]/div[1]/span[1]/input[1]').send_keys(drug['num'])
        self.dr.find_element(By.XPATH, '//*[@id="EAMform"]/div[1]/span[2]/input[1]').send_keys(drug['jbr_name'])
        self.dr.find_element(By.XPATH, '//*[@id="EAMform"]/div[2]/span[1]/input[1]').send_keys(drug['sex'])
        self.dr.find_element(By.XPATH, '//*[@id="EAMform"]/div[2]/span[2]/input[1]').send_keys(drug['tel'])
        self.dr.find_element(By.XPATH, '//*[@id="EAMform"]/div[3]/span/textarea').send_keys(drug['bz'])
        # 点击录入按钮
        time.sleep(0.2)
        self.dr.find_element(By.LINK_TEXT, '录入').click()
        time.sleep(1)
        # 断言- 查看弹窗中是否弹出录入成功
        try:
            msg = self.dr.find_element(By.XPATH, '//*[@class="messager-body panel-body panel-body-noborder window-body"]/div[0]').text
            ex = '保存成功'
            self.assertEqual(ex, msg, '出错啦')
        except Exception as e:
            Tools.save_screenshot(self.dr, 'drug_add')
            raise e